package com.huawei.M01.iterator.M24;

import java.util.Arrays;
import java.util.Map;

/**
 * 1626. 无矛盾的最佳球队
 *
 * @author 11
 */
public class BestTeam {
    public int bestTeamScore(int[] scores, int[] ages) {
        int bestScore = 0;
        int[][] members = new int[scores.length][2];
        for (int i = 0; i < scores.length; i++) {
            members[i] = new int[]{scores[i], ages[i]};
        }
        // 排序
        Arrays.sort(members, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
        int[] dp = new int[scores.length];

        // 动态查找最大值
        for (int i = 0; i < scores.length; i++) {
            for (int j = i - 1; j >= 0; --j) {
                if (members[j][1] <= members[i][1]) {
                    dp[i] = Math.max(dp[i], dp[j]);
                }
            }
            dp[i] += members[i][0];
            bestScore = Math.max(dp[i], bestScore);
        }
        return bestScore;
    }
}
